package com.fs.dao.Impl;

import com.fs.dao.UserDao;
import com.fs.pojo.User;
import com.fs.utils.JDBCUtils;
import com.fs.vo.UserVO;

import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public User queryById(String id) {

        List<User> users = JDBCUtils.executeQurey("select * from tb_user where  id = ?", User.class, id);

        return  users == null ? null : users.get(0);
    }

    @Override
    public List queryAll() {
        String sql = "select * from tb_user";
        return JDBCUtils.executeQurey(sql, User.class);
    }

    @Override
    public int insert(User obj) {

        String sql = "insert into tb_user (id, phone, password, number, name, avatar, school, role) values (?,?,?,?,?,?,?,?);";

        return JDBCUtils.executeUpdate(sql,obj);
    }

    @Override
    public int deleteById(String id) {
        String sql = "delete from tb_user where id = ?";
        return JDBCUtils.executeUpdate(sql,id);
    }

    @Override
    public int update(User obj) {
        String sql = "update tb_user set phone=?,number=?, name=?,avatar=?,school=? where id = ?";


        return JDBCUtils.executeUpdate(sql,obj.getPhone(),obj.getNumber()
                ,obj.getName(),obj.getAvatar(),obj.getSchool(),obj.getId());

    }

    @Override
    public User queryByPhone(String phone) {

        List<User> users = JDBCUtils.executeQurey("select * from tb_user where  phone=?", User.class, phone);

        return users==null ? null : users.get(0);
    }

    @Override
    public List<User> queryByClassId(String intClassId) {
        String sql = "select * from tb_user where id in (select student_id from tb_class_student where class_id = ?) and role = 1 ";
        return JDBCUtils.executeQurey(sql,User.class,intClassId);
    }

    @Override
    public List<User> queryStudentByNameOrNumber(String intclassId, String contition) {
        String sql = "select * from tb_user where id in (select student_id from tb_class_student where class_id = ?) and role = 1 and (name like ? or number like ?)";
        return JDBCUtils.executeQurey(sql,User.class,intclassId,"%"+contition+"%",contition+"%");
    }

    @Override
    public UserVO queryByIdReturnUserVO(String id) {
        List<UserVO> userVOS = JDBCUtils.executeQurey("select u.id,u.avatar,u.name,u.number,u.phone,u.role,u.school from tb_user u where  id = ?", UserVO.class, id);

        return  userVOS == null ? null : userVOS.get(0);
    }
}
